package org.compiere.process;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.acct.Doc;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MFI_C_PaymentLine;
import org.compiere.model.MPayment;
import org.compiere.model.Mhrmstatement;
import org.compiere.model.Mhrmstatementline;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class CreatePaymentForStatement extends SvrProcess {

	Mhrmstatement statement = null;
	/** Current context					*/
	private Properties m_ctx;
	
	@Override
	protected void prepare() {
		
		log.info("");
		m_ctx = Env.getCtx();
		
		statement = new Mhrmstatement(getCtx(), getRecord_ID(), get_TrxName());
		if(statement == null)
			throw new AdempiereException("Bad Statement or Record_ID is null");
	}

	@Override
	protected String doIt() throws Exception {

		MPayment payment = new MPayment(getCtx(), 0, get_TrxName());		
		Mhrmstatementline[] lines = statement.getLines();
		
		payment.setC_DocType_ID(statement.gethrm_settingsPayments().getC_DocType_ID());
		payment.setFI_TypeOfTax_ID(statement.gethrm_settingsPayments().getFI_TypeOfTax_ID());
		payment.setC_BPartner_ID(statement.gethrm_settingsPayments().getC_BPartner_ID());
		payment.setC_BankAccount_ID(statement.gethrm_settingsPayments().getC_BankAccount_ID());
		payment.setC_BP_BankAccount_ID(statement.gethrm_settingsPayments().getC_BP_BankAccount_ID());
		payment.setC_Currency_ID(statement.gethrm_settingsPayments().getC_BankAccount().getC_Currency_ID());		
		payment.setFI_PPC_ID(statement.gethrm_settingsPayments().getFI_PPC_ID());
		payment.setFI_Form3_ID(statement.gethrm_settingsPayments().getFI_Form3_ID());
		payment.setFI_Form3Explanation_ID(statement.gethrm_settingsPayments().getFI_Form3Explanation_ID());
		payment.setPayAmt(statement.gethrm_total());
		payment.setDateAcct(statement.getDateFrom());
		payment.sethrm_statement_ID(statement.gethrm_statement_ID());
		MAcctSchema as = MAcctSchema.get(m_ctx, Env.getContextAsInt(m_ctx, "$C_AcctSchema_ID"));
		payment.setC_ValidCombination_ID(getAccountSalary(as, Doc.ACCTTYPE_HRMSalary));
		payment.saveEx();
		
		for(Mhrmstatementline line: lines){
			MFI_C_PaymentLine pay = new MFI_C_PaymentLine(getCtx(), 0, get_TrxName());
			pay.setC_Payment_ID(payment.getC_Payment_ID());
			pay.setC_BPartner_ID(line.getC_BPartner_ID());
			pay.setAmount(line.getAmount());
			pay.saveEx();
			//
			line.setProcessed(true);
			line.saveEx();
		}
		
		statement.setC_Payment_ID(payment.getC_Payment_ID());
		statement.sethrm_PaymentProcess("Y");
		statement.saveEx();
		
		return Msg.translate(Env.getCtx(), "Success");
	}
	
	private final int getAccountSalary (MAcctSchema as, int acctType){
		String account = "";
		if(Doc.ACCTTYPE_HRMSalary == acctType)
			account = "3350%";  // деньги в пути
			
		
		String sql = "select c.c_validcombination_id \n" +
				"from c_validcombination c \n" +
				"inner join c_elementvalue e on e.c_elementvalue_id = c.account_id \n " +
				"where value like '"+account+"'";
		
		int C_ValidCombination_ID = 0;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			if (rs.next())
				C_ValidCombination_ID = rs.getInt(1);
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, "SQL=" + sql, e);
		}
		finally {
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return C_ValidCombination_ID;
	}	//	getAccount

}
